#include "kbsnewsrx.h"
#include "kbsconstants.h"

#include <QStandardItem>
#include <iostream>

QStandardItemModel* parseBoardIndex(const QString &htmlContent, QObject *parent)
{
    QStandardItemModel *model = new QStandardItemModel(1, 2, parent);

    QRegExp rx(KBS_BOARD_INDEX_RX); //, Qt::CaseSensitive, QRegExp::RegExp2);
    rx.setMinimal(true);
    QStringList list1, list2;
    int pos = 0, row = 0;

    while ((pos = rx.indexIn(htmlContent, pos)) != -1) {
        QString newsurl(KBS_NEWS_URL_PREFIX);
        newsurl += rx.cap(1);

        QString newstitle = rx.cap(2);

        QStandardItem *itemTitle = new QStandardItem(QString(newstitle));
        model->setItem(row, 0, itemTitle);

        if (row % 2 == 0) {
            itemTitle->setBackground(QBrush(QColor(222, 255, 222)));
        }
        else {
            itemTitle->setBackground(QBrush(QColor(191, 255, 191)));
        }

        QStandardItem *itemUrl = new QStandardItem(QString(newsurl));
        model->setItem(row, 1, itemUrl);

        //QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
        row++;
        pos += rx.matchedLength();
    }

    return model;
}

QString parseNewsContentScript(const QString &htmlContent)
{
    QRegExp rx(KBS_NEWS_TRANSCRIPT_RX);
    rx.setMinimal(true);

    QString scriptText;

    int pos = 0;
    while ((pos = rx.indexIn(htmlContent, pos)) != -1) {
        scriptText = rx.cap(1);
        pos += rx.matchedLength();
    }

    scriptText = scriptText.replace("<br />", "", Qt::CaseInsensitive).replace(QRegExp("^\\s*"), "").trimmed();

    return scriptText;
}

QString parseNewsContentMMS(const QString &htmlContent)
{
    QRegExp rx(KBS_NEWS_NORMAL_QUALITY_MMS_URL_RX);
    rx.setMinimal(true);

    QString mmsUrl(KBS_NEWS_MMS_URL_PREFIX);

    int pos = 0;
    while ((pos = rx.indexIn(htmlContent, pos)) != -1) {
        mmsUrl += rx.cap(1);
        pos += rx.matchedLength();
    }

    return mmsUrl;
}

QStringList parseNewsTranscript(const QString &transcript)
{
    QStringList result;
    QString copytrans(transcript);

    QRegExp cleanrx1("<.*>");
    cleanrx1.setMinimal(true);
    QRegExp cleanrx2("\\(.*\\)");
    cleanrx2.setMinimal(true);
    QRegExp cleanrx3("^.*:");
    cleanrx3.setMinimal(true);
    QRegExp trimrx("^\\s+");
    QRegExp shrinkspacerx("\\s\\s+");

    copytrans.replace(cleanrx1, "").replace(cleanrx2, "").replace("\"", "").replace(",", "");

    QStringList tmplist = copytrans.split(QRegExp("[\\n\\r\\.\\?]+"), QString::SkipEmptyParts);

    QStringList::const_iterator iter = tmplist.begin();

    while (iter != tmplist.end()) {
        QString str(*iter);
        str.replace(cleanrx3, "").replace(trimrx, "").replace(shrinkspacerx, " ");
        if (str != "")
            result << str;
        iter++;
    }

    return result;
}

QString getFileNameFromMmsUrl(const QString &url)
{
    QStringList slist = url.split("/");
    QString name;

    for (int i = 0; i < slist.count(); i++) {
        if (i >= 4 && i <= 6) {
            name.append(slist.at(i));
        }
        else if (i == 7) {
            name.append("-");
        }
        else if (i ==8) {
            name.append(slist.at(i));
        }
    }

    return name;
}
